//
//  HZNewsManager.m
//  car4S
//
//  Created by huazi on 14-3-19.
//  Copyright (c) 2014年 huifu. All rights reserved.
//

#import "HZNewsManager.h"
#import "HZSQLManager.h"
@implementation HZNewsManager
static HZNewsManager *shareNewsManager =nil;
+(HZNewsManager *)shareNewsManager
{
    @synchronized(self) {
        if ( shareNewsManager== nil)
        {
            // assignment not done here
            shareNewsManager =[[self alloc] init];
            
        }
        
    }
    return shareNewsManager;
}
+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self) {
        if (shareNewsManager == nil) {
            
            shareNewsManager = [super allocWithZone:zone];
            
            return shareNewsManager; // assignment and return on first allocation
        }
    }
    return nil; //on subsequent allocation attempts
}

- (id) init
{
    if (self =[super init])
    {
        HZSQLManager *SqlManager =[HZSQLManager shareSQLManager];
        NSString *strCreateTableSESSION =@"CREATE TABLE  IF NOT EXISTS SESSION ( oprID TEXT, isSelf TEXT, userId TEXT, msgType TEXT, content TEXT, downloadUrl TEXT,  timeLength TEXT, location TEXT, photoUrl TEXT,nickName TEXT, sendTime TEXT, bigpicUrl TEXT,smallpicUrl TEXT, noReadNum TEXT, lastUpdatetime TEXT)";
        [SqlManager execSql:strCreateTableSESSION];
        
        
        NSString *strCreateTableNEWS =@"CREATE TABLE  IF NOT EXISTS NEWS (ID INTEGER PRIMARY KEY AUTOINCREMENT, oprID TEXT, isSelf TEXT, userId TEXT, msgType TEXT, content TEXT, downloadUrl TEXT,  timeLength TEXT, location TEXT, photoUrl TEXT,nickName TEXT, sendTime TEXT,bigpicUrl TEXT,smallpicUrl TEXT, filePath TEXT ,isNoRead TEXT)";
        [SqlManager execSql:strCreateTableNEWS];
    }
    return self;
}

/*
-(void)addNewSOrAlterNewsFromSessionTable:(HZNews *)news
{
//    HZSQLManager *SQLManager =[HZSQLManager shareSQLManager];
//    Person *person =[Person sharePerson];
//    NSString *strQueryIsExist =[NSString stringWithFormat:@"SELECT * FROM SESSION WHERE (SESSION.oprID ='%@' AND SESSION.userId ='%@') ORDER BY lastUpdatetime DESC",person.userId,news.NuserId];
    
//    NSMutableArray *tempArray =[SQLManager querySessionTableIsExistNews:strQueryIsExist];
//    BOOL isExist=YES;
//    if (tempArray ==nil ||tempArray.count ==0)
//    {
//        isExist =NO;
//    }
//    if (isExist)
//    {
//        NSString *noReadNum =[[tempArray objectAtIndex:0] objectForKey:@"noReadNum"];
//        news.NnoReadNum =[NSString stringWithFormat:@"%i",[noReadNum intValue]+1];
//        [self alterNewsFromSessionTable:news];
//    }else
//    {
//        news.NnoReadNum =@"1";
//        [self addNewsToSessionTable:news];
//    }
    

}

-(void)addNewsToSessionTable:(HZNews *)news
{
   
    HZSQLManager *SQLManager =[HZSQLManager shareSQLManager];
    Person *person= [Person sharePerson];
    NSString *strINSRETINTO =[NSString stringWithFormat:@"INSERT INTO SESSION (oprID ,isSelf ,userId ,msgType ,content ,downloadUrl ,timeLength ,location ,photoUrl ,nickName ,sendTime ,bigpicUrl ,smallpicUrl, noReadNum , lastUpdatetime) VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",person.userId,news.NisSelf,news.NuserId,news.NmsgType,news.Ncontent,news.NdownloadUrl,news.NtimeLength,news.Nlocation,news.NphoneUrl,news.NnickName,news.NsendTime,news.NbigpicUrl,news.NsmallpicUrl,news.NnoReadNum,news.NsendTime];
    [SQLManager execSql:strINSRETINTO];
}
-(void)deleteNewsFromSessionTable:(HZNews *)news
{
    HZSQLManager *SQLManager =[HZSQLManager shareSQLManager];
    Person *person= [Person sharePerson];
    NSString * strDelete =[NSString stringWithFormat:@"DELETE FROM SESSION WHERE oprID ='%@' AND  userId='%@' ",person.userId,news.NuserId];
    [SQLManager execSql:strDelete];
}
-(void)alterNewsFromSessionTable:(HZNews *)news
{
    Person *person =[Person sharePerson];
    HZSQLManager *SQLManager =[HZSQLManager shareSQLManager];
    NSString *strAlter =[NSString stringWithFormat:@"UPDATE SESSION SET lastUpdatetime='%@',content='%@',msgType='%@',noReadNum='%@' WHERE (oprID='%@' AND userId='%@')",news.NsendTime,news.Ncontent,news.NmsgType,news.NnoReadNum, person.userId,news.NuserId];
    [SQLManager execSql:strAlter];
}


-(void)addNewsToNewsTable:(HZNews *)news
{
    HZSQLManager *SQLManager =[HZSQLManager shareSQLManager];
    Person *person= [Person sharePerson];
    NSString *strINSRETINTO =[NSString stringWithFormat:@"INSERT INTO NEWS (oprID ,isSelf ,userId ,msgType ,content ,downloadUrl ,timeLength ,location ,photoUrl ,nickName ,sendTime, bigpicUrl, smallpicUrl ,filePath ,isNoRead)VALUES(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",person.userId,news.NisSelf,news.NuserId,news.NmsgType,news.Ncontent,news.NdownloadUrl,news.NtimeLength,news.Nlocation,news.NphoneUrl,news.NnickName,news.NsendTime,news.NbigpicUrl,news.NsmallpicUrl,news.NfilePath,news.NisNoRead];
    [SQLManager execSql:strINSRETINTO];
}
-(void)deleteNewsFromNewsTable:(HZNews *)news
{

}
-(void)alterNewsFromNewsTable:(HZNews *)news
{

}
*/
@end
